/**
 * Created by dukes on 2015/12/22.
 */
class FirstRechargePanel extends eui.Component{
    private flag:boolean = false;
    public constructor(){
        super();
        this.skinName = "pet.FirstRechargePanelSkin";
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAdded, this );
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemoved, this);
    }

    public updateDisplay(){
        var firstConfig = ActivityMgr.getInstance().getRechargeConfigByIndex(0);
        this.lab_remind.textFlow = [
            {
            text:"首次充值只需",
            style:{
                "textColor": 0xda8503,
                "size":16
            }
        },
            {
                text:firstConfig.diamond+"",
                style:{
                    "textColor": 0xff0040,
                    "size":16
                }
            },
            {
                text:"钻石（人民币"+firstConfig.diamond/10+"元），\n即可获得以下奖励",
                style:{
                    "textColor": 0xda8503,
                    "size":16
                }
            }
        ];
        //var len = firstConfig.award_props.length;
        //if( len > 0){
        var propId;
        var idx = 0;
        var goodInfo:GoodInfo;
        for(var key in firstConfig.award_props){
            propId = parseInt(key);
            goodInfo = Model.GetGoodInfoById(propId);
            if( goodInfo.type == Model.GOOD_TYPE_ITEM){
                if( Model.GOOD_TYPE_CAT == Model.me.myType && !goodInfo.use4Cat){
                    continue;
                }else if( Model.GOOD_TYPE_DOG == Model.me.myType && !goodInfo.use4Dog){
                    continue;
                }
                this["item_"+idx].source = GameConst.Res_Root+"resource/assets/items/"+key+ ".png";
                this["lab_item_"+idx].textFlow = [
                    {
                        text:goodInfo.name,
                        style:{
                            "textColor": 0xda8503,
                            "size":16
                        }
                    },
                    {
                        text:"x"+firstConfig.award_props[key],
                        style:{
                            "textColor": 0xff0040,
                            "size":16
                        }
                    }
                ];
            }else if(goodInfo.type == Model.GOOD_TYPE_DECORATE){
                var dec = Model.GetDecDateById(propId);
                this["lab_item_"+idx].textFlow = [
                    {
                        text:dec.name,
                        style:{
                            "textColor": 0xda8503,
                            "size":16
                        }
                    },
                    {
                        text:"x"+firstConfig.award_props[key],
                        style:{
                            "textColor": 0xff0040,
                            "size":16
                        }
                    }
                ];
                this["item_"+idx].source = GameConst.Res_Root+"resource/assets/decorates/"+key+(goodInfo.classify != 4 ? ".png" :"-1.jpg");
            }


            if(goodInfo.term != 0){
                var timeLast = goodInfo.term*60*1000;
                if( timeLast > Model.DAY){
                    this["add_"+idx].text = goodInfo.descs+"\n时效:"+Math.round(timeLast/Model.DAY)+"天";
                }else{
                    this["add_"+idx].text = goodInfo.descs+"\n时效:"+GetTimeStr(timeLast);
                }
            }else{
                this["add_"+idx].text = goodInfo.descs+"\n时效:永久";
            }


            idx++;
        }
        if( idx <= 1){
            if(firstConfig.award_diamond!=0){
                this["item_"+idx].source = GameConst.Res_Root+"resource/assets/items/diamond.png";
                this["add_"+idx].text = "钻石x"+firstConfig.award_diamond;
                this["lab_item_"+idx].textFlow = [
                    {
                        text:"钻石",
                        style:{
                            "textColor": 0xda8503,
                            "size":16
                        }
                    },
                    {
                        text:"x"+firstConfig.award_diamond,
                        style:{
                            "textColor": 0xff0040,
                            "size":16
                        }
                    }];

                idx++;
            }

            if( idx <= 1){
                if(firstConfig.award_catHair != 0){
                    this["item_"+idx].source = GameConst.Res_Root+"resource/assets/items/cathair.png";
                    this["add_"+idx].text = "猫毛x"+firstConfig.award_catHair+"\n时效:永久";
                    this["lab_item_"+idx].textFlow = [
                        {
                            text:"猫毛",
                            style:{
                                "textColor": 0xda8503,
                                "size":16
                            }
                        },
                        {
                            text:"x"+firstConfig.award_catHair,
                            style:{
                                "textColor": 0xff0040,
                                "size":16
                            }
                        }];
                    idx++;
                }
            }

            if( idx <= 1){
                if(firstConfig.award_dogHair != 0){
                    this["item_"+idx].source = GameConst.Res_Root+"resource/assets/items/doghair.png";
                    this["add_"+idx].text = "狗毛x"+firstConfig.award_dogHair+"\n时效:永久";
                    this["lab_item_"+idx].textFlow = [
                        {
                            text:"狗毛",
                            style:{
                                "textColor": 0xda8503,
                                "size":16
                            }
                        },
                        {
                            text:"x"+firstConfig.award_dogHair,
                            style:{
                                "textColor": 0xff0040,
                                "size":16
                            }
                        }];
                    idx++;
                }
            }
        }

    }
    public onClose(evt:egret.TouchEvent){
        if( evt )
            SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        DisplayController.instance().removeFromAlert(GameConst.GrayMask);
        DisplayController.instance().removeFromAlert(this);

    }

    private onGet(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        if(ActivityMgr.getInstance().chargeInfo.first >= 10){
            window["__chargeAward"](1);
        }else{
            this.onClose(null);
            //SceneMgr.getInstance().switchScene(SceneMgr.SCENE_RECHARGE);
            BuyDiamondPanel.getInstance().show();
        }
    }

    private onAdded(evt:egret.Event){
        if( !this.flag){
            this.updateDisplay();
            this.flag = true;
        }
        this.img_get.source = ActivityMgr.getInstance().chargeInfo.first >= 10 ? "img_get" : "fr_recharge_btn_png";
        this.img_close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
        this.img_get.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGet, this);
    }

    private onRemoved(evt:egret.Event){
        this.img_close.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
        this.img_get.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onGet, this);
    }

    public show(){
        DisplayController.instance().addToAlert(GameConst.GrayMask);
        DisplayController.instance().addToAlert(this);
    }

    public img_close:eui.Image;
    public lab_remind:eui.Label;
    public item_0:eui.Image;
    public item_1:eui.Image;
    public lab_item_0:eui.Label;
    public lab_item_1:eui.Label;
    public add_0:eui.Label;
    public add_1:eui.Label;
    public img_get:eui.Image;
    public partAdded(name:string, instance:any){
        super.partAdded(name, instance);
        if( name == "img_close"){
            this.img_close = instance;
        }else if( name == "lab_remind"){
            this.lab_remind = instance;
        }else if( name == "item_0"){
            this.item_0 = instance;
        }else if( name == "item_1"){
            this.item_1 = instance;
        }else if( name == "lab_item_0"){
            this.lab_item_0 = instance;
        }else if( name == "lab_item_1"){
            this.lab_item_1 = instance;
        }else if( name == "add_0"){
            this.add_0 = instance;
        }else if( name == "add_1"){
            this.add_1 = instance;
        }else if( name == "img_get"){
            this.img_get = instance;
        }
    }
    public measure() {
        super.measure();
        this.x = GameConst.StageW - this.measuredWidth >> 1;
        this.y = GameConst.StageH - this.measuredHeight >> 1;
    }
    public childrenCreated(){
        super.childrenCreated();
        if( !this.flag){
            this.updateDisplay();
            this.flag = true;
        }
    }
    private static _instance:FirstRechargePanel;
    public static getInstance():FirstRechargePanel{
        if( FirstRechargePanel._instance == null)
            FirstRechargePanel._instance = new FirstRechargePanel();
        return FirstRechargePanel._instance;
    }
}